{extend name="common/add"}
{block name="form"}
<article class="page-container">
    <form action="{:str_replace('id',$role->role_id,url('/role/id'))}" method="post" enctype="multipart/form-data" class="form form-horizontal" id="form-member-add">

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">角色名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$role->role_name}" placeholder="请输入角色名称" id="role_name" name="role_name">
            </div>
        </div>

        <input type="hidden" name="_method" value="put" />

        <div class="row cl">
            <table class="table table-border table-bordered table-bg">
                <thead>
                <th>权限分类</th>
                <th>权限</th>
                </thead>
                <tbody>

                {php}
                $nowRoleAuth = explode(',',$role->role_ps_ids);
                {/php}

                {foreach $authFathers as $authFather}
                <tr>
                    <td>
                        {if in_array($authFather->auth_id,$nowRoleAuth)}
                        <input type="checkbox" name="auth_ids[]" value="{$authFather.auth_id}" checked class="chk">
                        {else /}
                        <input type="checkbox" name="auth_ids[]" value="{$authFather.auth_id}" class="chk">
                        {/if}
                        {$authFather->auth_name}
                    </td>

                    <td>
                        {foreach $authSons as $authSon}
                        {if $authSon->auth_pid == $authFather->auth_id}



                        {if in_array($authSon->auth_id,$nowRoleAuth)}
                        <input type="checkbox" name="auth_ids[]" value="{$authSon.auth_id}" checked class="child">
                        {else /}
                        <input type="checkbox" name="auth_ids[]" value="{$authSon.auth_id}" class="child">
                        {/if}

                        {$authSon->auth_name}


                        {/if}
                        {/foreach}
                    </td>

                </tr>
                {/foreach}
                </tbody>
            </table>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>
{/block}


{block name="js"}

<script type="text/javascript">
    $('.chk').click(function () {
        var status = $(this).prop('checked')
        if (status == true) {
            console.log(status)
            $(this).parent().next().children().prop('checked', true)
        } else {
            $(this).parent().next().children().prop('checked', false)
        }
    })


    //补选父级
    $('.child').click(function () {
        var currentChildStatus = $(this).prop('checked')
        var parent = $(this).parent().prev().children()

        if (currentChildStatus == true) {
            $(this).parent().prev().children().prop("checked", true)
        } else {
            var selChkBoxs = $(this).parent().children()

            var num = 0;
            $.each(selChkBoxs, function (k, v) {
                if ($(v).prop('checked')) {
                    num++;
                }
            })

            if(num == 0){
                $(this).parent().prev().children().prop("checked", false)
            }
        }
    });
</script>
{/block}